package gov.kqqzyyy.monkey.tjpd.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.util.Collections;
import java.util.List;

import gov.kqqzyyy.monkey.tjpd.R;
import gov.kqqzyyy.monkey.tjpd.mytools.MyStringUtil;
import gov.kqqzyyy.monkey.tjpd.objects.TJ_DDDL;

public class PatientAdapter extends BaseAdapter {

    private LayoutInflater inflater;
    private List<TJ_DDDL> list;

    public PatientAdapter(Context context) {
        this.inflater = LayoutInflater.from(context);
    }

    public void setList(List<TJ_DDDL> list) {
        Collections.sort(list, (arg0, arg1) -> arg0.getDdh() - arg1.getDdh());
        this.list = list;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        if (list != null && list.size() > 0) {
            return list.size();
        }
        return 0;
    }

    @Override
    public Object getItem(int i) {
        if (getCount() > 0 && list.get(i) != null) {
            return list.get(i);
        }
        return null;
    }


    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = inflater.inflate(R.layout.item_patient, parent, false);
            holder.xhText = convertView.findViewById(R.id.item_xh);
            holder.tjhText = convertView.findViewById(R.id.item_tjh);
            holder.xmText = convertView.findViewById(R.id.item_xm);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        if (list != null && list.size() > 0) {
            TJ_DDDL tjDddl = list.get(position);
            String xh = "排队号:" + tjDddl.getDdh();
            holder.xhText.setText(xh);
            String tjh = "编号:" + tjDddl.getDdbh();
            holder.tjhText.setText(tjh);
            String xm = MyStringUtil.doHideName(tjDddl.getDdry());
            holder.xmText.setText(xm);
        }
        return convertView;
    }

    private class ViewHolder {
        private TextView xhText;
        private TextView tjhText;
        private TextView xmText;
    }
}
